import Data.List
import System.IO
import System.Environment


main = do
  

    let list1 = take1 5 ["1","+","3","-","5", "+", "6","*","8","-","10"]
    print list1
    
    let list2 = take2 5 "1 + 3 + 4 - 5 + 6"
    print list2
    




    let para1 = ["="]
    print para1
     
    let list3 = take3 [] ["4","+","3","+","5"]
    print list3
    
    let list3a = take3a [] ["1","+","13","+","5"]
    print list3a
    
   

    
    let cc = addstr "3" "7"
    print cc
    
    let cc1 = addstr1 "3" "7"  ["1", "2"]
    print "cc1="
    print cc1
    
    
    let list3b = take3b list3  list3a
    print "list3b="
    print list3b

    
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs)
    | x > maxTail = x
    | otherwise = maxTail
    where maxTail = maximum' xs 


sum' :: (Num a) => [a] -> a
sum' [] = error "sum of empty list"
sum' [x] = x
sum' (x:xs) = x + sum(xs)

{-
fun1' :: (Num a) => [a]-> String -> [a]
fun1' xs = fun2' xs . words

    
fun2' :: (Num a, Num b, Read b) => [a]-> [b]-> [a]
--fun2' xs xs2 = [1,2,3]
fun2' xs x =   read x : xs
--fun2' xs (x:xs2) = [1,2,3]

--fun2' x xs2 = xs2:x
--un2' (x:xs)  xs2 = fun2'  xs  (xs2:x)
-}   

take' :: (Num i, Ord i) => i -> [a] -> [a]
take' n _
    | n <= 0 = []
take' _ [] = []
take' n (x:xs) = x : take' (n-1) xs

{-
take1 :: (Num i, Ord i) => i -> [String] -> [String]
take1 n _
    | n <= 0 = []
take1 _ [] = []
take1 n ("+":xs) =  take1 n xs
take1 n ("-":xs) =  take1 n xs
take1 n ("*":xs) =  take1 n xs
take1 n ("/":xs) =  take1 n xs
take1 n (x:xs) =  x : take1 n xs
-}

take2 :: (Num i, Ord i) => i -> String -> [String]
take2  n xs = take1 n (words xs)
    
    
take1 :: (Num i, Ord i) => i -> [String] -> [String]
take1 n _
    | n <= 0 = []
take1 _ [] = []
take1 n (x:xs)
    | x `elem` ["+","-", "*", "/"] = take1 n xs
    | otherwise = x : take1 n xs
    
    
take3 ::[String] -> [String] -> [String ]
take3 _ [] = []
take3 n (x:xs)
    | x `elem` ["+","-", "*", "/"] = take3 (x:n) xs
    | otherwise = x : take3 n xs    
    
take3a ::[String] -> [String] -> [String ]
take3a _ [] = []
take3a n (x:xs)
    | x `elem` ["+","-", "*", "/"] = x : take3a n xs
    | otherwise = take3a n xs    
    
    
take3b ::[String] -> [String] -> [String]
take3b [x] [] = [x ++ "dddd"]
take3b [] _ = []
--take3b x n = 
--take3b (x1:x2) ("+":xs2) = take3b  (read x1 ) xs2
--take3b (x1:x2) ("-":xs2) = take3b  (read x1  ) xs2
take3b (x:y:ys) ("+":xs) = take3b  (addstr1 x y ys)  xs
take3b (x:y:ys) ("-":xs) = take3b  ys xs
take3b x n = x

addstr :: String -> String->String
addstr a b = show (aa + bb)
    where   aa = read a
            bb = read b
            
addstr1 :: String -> String->[String]->[String]
addstr1 x y ys = z:ys
    where   z = addstr x y    
    

            
                
    
take4 ::[String] -> [String] -> [String ]
take4 n [] = []
take4 n ("+":xs) =  take4 ("+":n) xs
take4 n ("-":xs) =  take4 ("+":n) xs
take4 n (x:xs) = x : take4 n xs      

take4a ::[String] -> [String] -> [String ]
take4a n [] = []
take4a n ("+":xs) =  take4 ("+":n) xs
take4a n ("-":xs) =  take4 ("+":n) xs
take4a n (x:xs) = x : take4 n xs     
  
--take1 n x =  x 
--take1 n (x:xs) =  x : take' (n-1) xs
-- x `elem`  ["0123456789"]  = x : take1 n xs
{-
take1 n (x:xs)
    | x `elem` ["+-*"] =  take1 n xs
    | otherwise = take1 n xs
-}


